/*
 *	Keyboard standard I/O module (limited version) for BCE
 *
 *	Copyright (C) 2011 XiaoJSoft Studio. All Rights Reserved.
 *	Copyright (C) Ji WenCong <whs_jwc@163.com>
 *
 *	This program is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.
 *
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include "../include/console/keybio.h"
#include "../include/strext.h"

/*
 *	char* inControl();
 *
 *	A full realize of scanf(&x, "%s");
 *
 */
char* inControl()
{
	char *io = NULL;
	char pzIO;
	int count = 0;

	io = NULL;
	count = 0;

	do {
		scanf("%c", &pzIO);
		if (pzIO != '\n') {
			io = (char*) realloc(io, (++count) * sizeof(char));
			if (io == NULL) {
				return(NULL);
			}
			*(io + count - 1) = pzIO;
		}
	} while(pzIO != '\n');

	io = (char*) realloc(io, (++count) * sizeof(char));
	if (io == NULL) {
		return(NULL);
	}
	*(io + count - 1) = '\0';

	return(io);
}

/*
 *	int inControlNumber();
 *
 *	A full realize of scanf(&x, "%d");
 *
 */
int inControlNumber()
{
	char *io;
	int cvt;

	io = NULL;

	do {
		if (io != NULL) {
			free(io);
			io = NULL;
		}
		io = inControl("", "");
	} while (isNumeric(io) == 0);

	cvt = atoi(io);

	free(io);

	return(cvt);
}

/*
 *	double inControlDouble();
 *
 *	A full realize of scanf(&x, "%f");
 *
 */
double inControlDouble()
{
	char *io;
	double cvt;

	io = NULL;

	do {
		if (io != NULL) {
			free(io);
			io = NULL;
		}
		io = inControl("", "");
	} while (isNumeric(io) == 0);

	cvt = strtod(io, NULL);

	free(io);

	return(cvt);
}

